package Client;

import SQL.Connections;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;
import java.util.Vector;


public class AdminPage extends JFrame{

    private static JFrame frame;
    private JPanel j1;
    private JTable table1;
    private JButton 发布Button;
    private JButton 查看Button;
    private JButton 删除Button;
    private JButton 班次找人Button;
    private JButton 身份证找人Button;
    private JFormattedTextField formattedTextField1;
    private JFormattedTextField formattedTextField2;
    private JOptionPane jOptionPane;

    private Vector<Vector> data;
    private Vector<String> title;
    private int row; //记录JTable的行数
    private int rowIndex;
    private String classNo;
    private String IdCard;


    public AdminPage() {
        发布Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AdminPost.initialize();
                frame.dispose();
            }
        });

        table1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //super.mouseClicked(e);
                rowIndex = table1.getSelectedRow();
                row = table1.getRowCount();
                classNo = (String) data.get(rowIndex).get(0);
            }
        });
        查看Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                AdminView.initialize(classNo);
                frame.dispose();
            }
        });
        删除Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Connections connections = new Connections();
                    Connection con = connections.getConnection();
                    Statement stm = con.createStatement();
                    String sqlTicket = "delete from Ticket where TClassNumber = '" + classNo + "'";
                    String sqlClasses = "delete from Classes where ClassNumber = '"+classNo+"'";
                    int rsTicket = stm.executeUpdate(sqlTicket);
                    int rsClasses = stm.executeUpdate(sqlClasses);
                    if(rsClasses == 1){
                        jOptionPane.showMessageDialog(null, "删除成功", "系统提示", JOptionPane.INFORMATION_MESSAGE);
                    }
                    frame.dispose();
                    AdminPage.initialize();
                }catch (Exception ex){
                    jOptionPane.showMessageDialog(null, "删除成功", "系统提示", JOptionPane.INFORMATION_MESSAGE);
                    ex.printStackTrace();
                }
            }
        });
        班次找人Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String classNo = formattedTextField1.getText();
                frame.dispose();
                AdminPage.initialize(classNo);
            }
        });
        身份证找人Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                IdCard = formattedTextField2.getText();
                AdminFindPeople.initialize(IdCard);
                frame.dispose();

            }
        });
    }

    public static void main(String[] args) {

        initialize();
    }

    public static void initialize() { //创建并初始化管理员界面
        //创建管理员界面
       AdminPage adminPage = new AdminPage();
       //保存管理员界面的地址
       adminPage.frame = adminPage;
       //设置管理员界面的标题
       adminPage.setTitle("管理员界面");
       //设置窗口的基础面板，显示窗口内容
       adminPage.setContentPane(adminPage.j1);
       adminPage.setDefaultCloseOperation(adminPage.EXIT_ON_CLOSE);
       //设置窗口居中
        adminPage.setLocationRelativeTo(null);
       adminPage.pack();
       //将管理员界面设置为可见
       adminPage.setVisible(true);

       //以下为初始化JTable
        adminPage.data = new Vector<>();
        adminPage.title = new Vector();
        String sql = "select ClassNumber,ClassDepaturePlace,ClassGoalPlace,ClassDepatureTime from classes";
        adminPage.data = Connections.queryData(sql);
        adminPage.title.add("班次号");
        adminPage.title.add("出发地点");
        adminPage.title.add("目的地");
        adminPage.title.add("日期时间");
        DefaultTableModel dtm = new DefaultTableModel(adminPage.data,adminPage.title);
        adminPage.table1.setModel(dtm);
        dtm.setDataVector(adminPage.data,adminPage.title);
        adminPage.table1.setRowSelectionInterval(0,0);

    }

    public static void initialize(String classNo) { //通过班次筛选出乘客
        if(classNo.equals("")){
            frame.dispose();
            AdminPage.initialize();
        }else{
            //创建管理员界面
            AdminPage adminPage = new AdminPage();
            //保存管理员界面的地址
            adminPage.frame = adminPage;
            //设置管理员界面的标题
            adminPage.setTitle("管理员界面");
            //设置窗口的基础面板，显示窗口内容
            adminPage.setContentPane(adminPage.j1);
            adminPage.setDefaultCloseOperation(adminPage.EXIT_ON_CLOSE);
            //设置窗口居中
            adminPage.setLocationRelativeTo(null);
            adminPage.pack();
            //将管理员界面设置为可见
            adminPage.setVisible(true);

            //以下为初始化JTable
            Vector<Vector> data = new Vector<Vector>();
            Vector<String> title = new Vector();
            String sql = "select ClassTrainNumber,ClassNumber,ClassDepaturePlace,ClassGoalPlace,ClassDepatureTime,IdCardName,IdCardSex,IdCardNumber " +
                    " from IdCard,Ticket,Classes where IdCardNumber=TIdCardNumber and TClassNumber=ClassNumber and ClassNumber = '"+classNo+"'";
            data = Connections.queryData(sql);
            title.add("火车号");
            title.add("班次号");
            title.add("出发地点");
            title.add("目的地");
            title.add("日期时间");
            title.add("乘客姓名");
            title.add("性别");
            title.add("身份证");
            DefaultTableModel dtm = new DefaultTableModel(data,title);
            adminPage.table1.setModel(dtm);
            dtm.setDataVector(data,title);
            adminPage.table1.setRowSelectionInterval(0,0);
        }

    }


    private void createUIComponents() {
        // TODO: place custom component creation code here
    }


}